package edu.uoc.tds.pac4.client;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JPasswordField;
import java.awt.event.*;
import edu.uoc.tds.pac4.beans.Usuari;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;

public class PantallaIdentificacio extends JDialog implements PanelClientInterface {
	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JTextField jUserField = new JTextField();
	private JPasswordField jPasswordField = new JPasswordField();
	private JPanel jPanelContainer = null;
	private JButton jButtonCancelar = null;
	private JLabel jLabelImage = null;
	private JLabel jLabelText = null;
	protected Usuari usuari = null;
	protected RemoteInterface remot;
	private Client client;

	public PantallaIdentificacio(){
		super();
		initialize();
	}

	public PantallaIdentificacio(Frame PantallaClientMenu, Client client, RemoteInterface remot)
	{	
		super(PantallaClientMenu, true);
		this.client = client;
		this.remot = remot;
		initialize();
	}

	private void initialize()
	{		
		this.setTitle(TDSLanguageUtils.getMessage("Client.PantallaIdentificacio.Title"));
		this.setContentPane(getJPanelContainer(new Dimension(700, 500)));
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		// Tamany del JDialog
		this.setSize(500,400);
		this.setLocationRelativeTo(this);
		this.setVisible(true);
		this.pack();

	}
	/**
	 * This method initializes jPanelContainer	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.ipadx = 430;
			gridBagConstraints.ipady = 250;
			gridBagConstraints.gridy = 0;
			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanel(), gridBagConstraints);
			jPanelContainer.setSize(new Dimension(800, 600));			
		}
		return jPanelContainer;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabelText = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaIdentificacio.Title"));
			jLabelText.setBounds(new Rectangle(160, 15, 230, 30));
			jLabelText.setFont(new Font("Arial", Font.PLAIN, 18));
			jLabelText.setForeground(new Color(0x8B008B));
			ImageIcon imagenExterna = new ImageIcon(this.getClass().getResource("/img/login.png"));
			jLabelImage = new JLabel(imagenExterna);
			jLabelImage.setBounds(new Rectangle(10, 40, 128, 128));
			jLabel1 = new JLabel();
			jLabel1.setText(TDSLanguageUtils.getMessage("Client.PantallaIdentificacio.User"));
			jLabel1.setBounds(new Rectangle(150, 80, 100, 25));
			jLabel = new JLabel();
			jUserField.setBounds(new Rectangle(250, 80, 150, 25));
			jLabel.setText(TDSLanguageUtils.getMessage("Client.PantallaIdentificacio.Clau"));
			jLabel.setBounds(new Rectangle(150, 125, 100, 25));
			jPanel = new JPanel();
			jPasswordField.setEchoChar('*');
			jPasswordField.setBounds(new Rectangle(250, 125, 150, 25));
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(getJButtonAcceptar(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(jUserField, null);
			jPanel.add(jPasswordField, null);
			jPanel.add(getJButtonCancelar(), null);
			jPanel.add(jLabelImage, null);
			jPanel.add(jLabelText, null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAcceptar() {

		if (jButton == null) {
			jButton = new JButton("Acceptar");
			jButton.setBounds(new Rectangle(85, 195, 100, 25));
			jButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						checkUsuari();
						dispose();
					} catch (Exception e){						
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton("Cancelar");
			jButtonCancelar.setBounds(new Rectangle(240, 195, 100, 25));
			jButtonCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					jUserField.setText("");
					jPasswordField.setText("");
					jUserField.requestFocus();
					setVisible(true);					
				}
			});
		}
		return jButtonCancelar;
	}

	private void checkUsuari() {
		String user = jUserField.getText();
		char[] c = jPasswordField.getPassword();
		String clau = new String(c);
		try {
			usuari=remot.loginUsuari(user, clau);			
			client.setUserConnected(usuari);
			client.setOK(true);
			new PantallaError("Info_benvingut", PantallaError.Tipus.INFO);        
		} catch (Exception e){
			new PantallaError("Error_check_user", PantallaError.Tipus.ERROR);
		}
	}

	public Usuari getUserConnected(){
		return usuari;
	}

	public boolean isOk(){
		if (usuari!=null)
			return true;
		else
			return false;
	}		
}